package com.chenyaowei.web.controller.product.spu;

import com.chenyaowei.common.annotation.Anonymous;
import com.chenyaowei.common.annotation.Log;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.core.page.TableDataInfo;
import com.chenyaowei.common.enums.BusinessType;
import com.chenyaowei.common.utils.poi.ExcelUtil;
import com.chenyaowei.product.bo.CommentLevelTotalBO;
import com.chenyaowei.product.domain.CywProSpuComment;
import com.chenyaowei.product.request.ProductCommentRequest;
import com.chenyaowei.product.service.ICywProSpuCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品评价Controller
 *
 * @author 陈耀威
 * @date 2023-12-03
 */
@RestController
@RequestMapping("/product/spuComment")
public class CywProSpuCommentController extends BaseController
{
    @Autowired
    private ICywProSpuCommentService cywProSpuCommentService;


    /**
     * 查询spu所有的评价
     */
    @Anonymous
    @GetMapping
    public TableDataInfo listBySpuId(@Validated ProductCommentRequest productCommentRequest)
    {
        // 统计好评、差评、xxx数量
        Long spuId = productCommentRequest.getSpuId();
        CommentLevelTotalBO commentLevelTotalBO = cywProSpuCommentService.countCommentLevelTotal(spuId);
        // 查询评价
        CywProSpuComment cywProSpuComment = new CywProSpuComment();
        cywProSpuComment.setSpuId(productCommentRequest.getSpuId());
        cywProSpuComment.setShowStatus(1);
        Map<String, Object> params = new HashMap<>();
        params.put("queryCondition", productCommentRequest.getQueryCondition());
        cywProSpuComment.setParams(params);
        startPage();
        List<CywProSpuComment> list = cywProSpuCommentService.selectCywProSpuCommentList(cywProSpuComment);
        TableDataInfo dataTable = getDataTable(list);
        params.clear();
        params.put("statistics", commentLevelTotalBO);
        dataTable.setExtra(params);
        return dataTable;
    }

    /**
     * 查询用户自己所有的评价
     */
    @GetMapping("/list/currentUser")
    public TableDataInfo listByMemberId(CywProSpuComment cywProSpuComment)
    {
        cywProSpuComment.setMemberId(getUserId());
        cywProSpuComment.setShowStatus(1);
        startPage();
        List<CywProSpuComment> list = cywProSpuCommentService.selectCywProSpuCommentList(cywProSpuComment);
        return getDataTable(list);
    }

    /**
     * 查询商品评价列表
     */
//    @PreAuthorize("@ss.hasPermi('product:spuComment:list')")
    @GetMapping("/list")
    public TableDataInfo list(CywProSpuComment cywProSpuComment)
    {
        startPage();
        List<CywProSpuComment> list = cywProSpuCommentService.selectCywProSpuCommentList(cywProSpuComment);
        return getDataTable(list);
    }

    /**
     * 导出商品评价列表
     */
    @PreAuthorize("@ss.hasPermi('product:spuComment:export')")
    @Log(title = "商品评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CywProSpuComment cywProSpuComment)
    {
        List<CywProSpuComment> list = cywProSpuCommentService.selectCywProSpuCommentList(cywProSpuComment);
        ExcelUtil<CywProSpuComment> util = new ExcelUtil<CywProSpuComment>(CywProSpuComment.class);
        util.exportExcel(response, list, "商品评价数据");
    }

    /**
     * 获取商品评价详细信息
     */
//    @PreAuthorize("@ss.hasPermi('product:spuComment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cywProSpuCommentService.selectCywProSpuCommentById(id));
    }

    /**
     * 新增商品评价（CywOrderItemController）
     */
/*    @PreAuthorize("@ss.hasPermi('product:spuComment:add')")
    @RepeatSubmit
    @Log(title = "商品评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CywProSpuComment cywProSpuComment)
    {
        return toAjax(cywProSpuCommentService.insertCywProSpuComment(cywProSpuComment));
    }*/

    /**
     * 修改商品评价
     */
    @PreAuthorize("@ss.hasPermi('product:spuComment:edit')")
    @Log(title = "商品评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CywProSpuComment cywProSpuComment)
    {
        return toAjax(cywProSpuCommentService.updateCywProSpuComment(cywProSpuComment));
    }

    /**
     * 删除商品评价
     */
    @PreAuthorize("@ss.hasPermi('product:spuComment:remove')")
    @Log(title = "商品评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cywProSpuCommentService.deleteCywProSpuCommentByIds(ids));
    }
}
